package main

import (
	"flag"
	"fmt"
	"gitee.com/liumou_site/gcd/model"
	"os"
	"path/filepath"
)

var (
	url   string
	ver   bool
	show  bool
	Total int
	Save  string
	Key   string
	Debug bool
)

func main() {
	version := "1.2.4"
	urls := os.Getenv("URL")
	if len(urls) <= 7 {
		urls = "https://url.liumou.site"
	}
	Key = os.Getenv("UrlKey")
	pwd, _ := os.Getwd()
	defaultPwd := filepath.Join(pwd, "Download")
	gfs := flag.NewFlagSet("urls", flag.ExitOnError)
	gfs.StringVar(&url, "u", urls, "设置接口地址")
	gfs.StringVar(&Save, "s", defaultPwd, "设置文件保存路径")
	gfs.StringVar(&Key, "k", Key, "设置Key值")
	gfs.IntVar(&Total, "t", 5, "设置同时下载文件数量")
	gfs.BoolVar(&Debug, "d", false, "开启debug模式")
	gfs.BoolVar(&ver, "v", false, "显示版本号")
	gfs.BoolVar(&show, "show", false, "查看程序简介")
	args := os.Args
	err := gfs.Parse(args[1:])
	if err != nil {
		return
	}
	if ver {
		fmt.Println(version)
		os.Exit(0)
	}
	if show {
		fmt.Println("这是一个通过接口获取需要下载的超链接进行文件下载的程序" +
			"\n作者：坐公交也用券" +
			"\n主页: https://liumou.site")
		os.Exit(0)
	}
	if url == "" {
		fmt.Println("请设置接口地址")
		os.Exit(2)
	}
	if Total < 1 {
		fmt.Println("设置的下载文件数量不能小于1")
		os.Exit(0)
	}
	w := model.NewDownLoad(url)
	w.Pwd = pwd
	w.Key = Key
	w.Thread = Total
	w.SavePath = Save
	w.Version = version
	w.Debug = Debug
	for {
		w.Start()
	}
}
